package com.filepublic.demo.server.controller;

import com.filepublic.demo.common.result.Result;
import com.filepublic.demo.pojo.dto.EmailDto;
import com.filepublic.demo.pojo.entity.FileEntity;
import com.filepublic.demo.pojo.vo.EmailVo;
import com.filepublic.demo.server.service.EmailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/email")
@Api(tags = "邮件控制器")
public class EmailController {

    @Resource
    EmailService emailService;

    /**
     * 发送邮件
     * @param file
     * @param emailOwnerId
     * @param title
     * @param fileId
     * @return
     */
    @ApiOperation(value = "发送邮件")
    @PostMapping("/send")
    public Result sendEmail(
            @RequestParam("file") MultipartFile file,
            @RequestParam("emailOwnerId") String emailOwnerId,
            @RequestParam("title") String title,
            @RequestParam("fileId") String fileId,
            @RequestParam("text") String text
            ) throws IOException {
        EmailDto emailDto = EmailDto.builder()
                .emailOwnerId(emailOwnerId)
                .fileId(fileId)
                .file(file)
                .title(title)
                .text(text)
                .build();
        emailService.sendEmail(emailDto);
        return Result.success();
    }

    /**
     * 获取邮件列表
     * @return
     */
    @GetMapping("/list")
    public Result<List<EmailVo>> listEmail(){
        List<EmailVo> emailVoList = emailService.listEmail();
        return Result.success(emailVoList);
    }

    /**
     * 获取文件信息
     */
    @GetMapping("/fileInfo/{fileId}")
    public Result<FileEntity> getFileInfo(@PathVariable("fileId") String fileId){
        FileEntity fileEntity = emailService.getFileInfo(fileId);
        return Result.success(fileEntity);
    }

    /**
     * 已读
     * @param emailId
     * @return
     */
    @PutMapping("/read/{emailId}")
    public Result readEmail(@PathVariable("emailId") Integer emailId){
        emailService.readEmail(emailId);
        return Result.success();
    }

    /**
     * 收藏
     */
    @PutMapping("/star/{emailId}/{state}")
    public Result starEmail(@PathVariable("emailId") Integer emailId,@PathVariable("state") Integer state){
        emailService.starEmail(emailId,state);
        return Result.success();
    }
}
